home *** CD-ROM | disk | FTP | other *** search
- /*
- File: StorageLibrary.c
-
- Contains: Graphics library for primitive flattening of QuickDraw GX data.
-
- Written by: Cary Clark, Georgiann Delaney, Michael Fairman, Dave Good,
- Robert Johnson, Keith McGreggor, Oliver Steele, David Van Brink,
- Chris Yerga
-
- Copyright: © 1995-1997 by Apple Computer, Inc., all rights reserved.
-
- Writers:
-
- (jtd) John Daggett
- (DH) David Hayward
- (cam) Cameron Esfahani
- (IK) Ingrid Kelly
-
- Change History (most recent first):
-
- <6> 6/1/97 IK Modify printing routines for GXGraphics 1.1.6.
- <5> 4/8/97 DH Added ShapeToFSSpec and FSSpecToShape.
- <4> 10/20/95 jtd Change ScalerTypes.h ScalerStreamTypes.h.
- <3> 9/21/95 cam Change BlockMove's to BlockMoveData's.
- <2> 5/1/95 jtd Bring in final 1.1 source changes.
- <1> 1/9/95 jtd First checked in.
- */
-
- #include <Errors.h>
- #include <Files.h>
- #include <GXFonts.h>
- #include <GXGraphics.h>
- #include <ScalerTypes.h>
-
- #include "StorageLibrary.h"
-
- /* ---------------------------------------------------------------------------
- The following routine is an example of how to write a spooling procedure to
- work with GXFlattenShape and GXUnflattenShape. For writing, it stores the data in
- a Handle which must be disposed of after the GXFlattenShape call. When reading,
- it expects the 'data' field of the gxSpoolBlock to have a handle to the data to be
- unflattened.
- --------------------------------------------------------------------------- */
-
- #define allocationIncrement 1024 /* storage handle is grown by this amount */
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- static long HandleSpoolProc(gxSpoolCommand command, userSpool *block)
- {
- switch (command)
- {
- case gxOpenReadSpool:
- block->size = 0;
- block->position = 0;
- break;
-
- case gxOpenWriteSpool:
- block->data = NewHandle(allocationIncrement);
- block->size = allocationIncrement;
- block->position = 0;
- break;
-
- case gxReadSpool:
- BlockMoveData((*(char **) block->data) + block->position, block->spool.buffer, block->spool.count);
- block->position += block->spool.count;
- break;
-
- case gxWriteSpool:
- {
- register long oldPosition;
-
- oldPosition = block->position;
- block->position += block->spool.count;
-
- /* Make sure there is at least enough room for one buffer size past current pointer. */
-
- if (block->position + block->spool.bufferSize > block->size)
- {
- block->size += block->spool.bufferSize;
- HUnlock((Handle) block->data);
- SetHandleSize((Handle) block->data, block->size);
- HLock((Handle) block->data);
- }
- BlockMoveData(block->spool.buffer, (*(char **) block->data + oldPosition), block->spool.count);
- break;
- }
-
- case gxCloseSpool:
- SetHandleSize((Handle) block->data, block->position);
- break;
- }
-
- return 0L;
- }
-
- #ifdef __cplusplus
- }
- #endif
-
- /* ---------------------------------------------------------------------------
- This routine spools to and from a file. The file must have previously been created and opened,
- and its refNum is expected to be in the refnum field of the block.
- --------------------------------------------------------------------------- */
-
- static long FileSpoolProc(gxSpoolCommand command, userSpool *block)
- {
- short err;
-
- switch (command) {
- case gxOpenReadSpool:
- case gxOpenWriteSpool:
- break;
-
- case gxReadSpool:
- {
- long count = block->spool.count;
- err = FSRead(block->reference, &count, block->spool.buffer);
- // IfDebug(err && err != eofErr, "\pFSRead failed");
- break;
- }
-
- case gxWriteSpool:
- {
- long count = block->spool.count;
- err = FSWrite(block->reference, &count, block->spool.buffer);
- // IfDebug(err, "\pFSWrite failed");
- break;
- }
-
- case gxCloseSpool:
- break;
-
- default:
- // IfDebug(true, "\punexpected spool command");
- break;
- }
-
- return 0L;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- Handle GXStShapeToHandle(gxShape source)
- {
- return GXStShapeToHandleWithFlags(source, gxFontListFlatten | gxFontGlyphsFlatten);
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- Handle GXStShapeToHandleWithFlags(gxShape source, gxFlattenFlag flags)
- {
- userSpool block;
-
- if (source == nil)
- {
- GXPostGraphicsError(shape_is_nil);
- return nil;
- }
-
- block.spool.spoolProcedure = NewgxSpoolProc(HandleSpoolProc);
- block.spool.buffer = nil;
- block.spool.bufferSize = 0;
- GXFlattenShape(source, flags, &block.spool);
- DisposeRoutineDescriptor(block.spool.spoolProcedure);
- return (Handle) block.data;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- gxShape GXStHandleToShape(Handle source, long count, const gxViewPort portList[])
- {
- userSpool block;
- gxShape dest;
-
- block.spool.spoolProcedure = NewgxSpoolProc(HandleSpoolProc);
- block.spool.buffer = nil;
- block.spool.bufferSize = 0;
- block.data = source;
- dest = GXUnflattenShape(&block.spool, count, portList);
- DisposeRoutineDescriptor(block.spool.spoolProcedure);
- return dest;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- void GXStShapeToFRef(gxShape source, short refNum)
- {
- userSpool block;
-
- if (source == nil)
- {
- GXPostGraphicsError(shape_is_nil);
- return;
- }
-
- block.spool.spoolProcedure = NewgxSpoolProc(FileSpoolProc);
- block.reference = refNum;
- block.spool.buffer = nil;
- block.spool.bufferSize = 0;
- GXFlattenShape(source, gxFontListFlatten | gxFontGlyphsFlatten, &block.spool);
- DisposeRoutineDescriptor(block.spool.spoolProcedure);
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- gxShape GXStFRefToShape(short refNum, long count, const gxViewPort portList[])
- {
- userSpool block;
- gxShape dest;
-
- block.spool.spoolProcedure = NewgxSpoolProc(FileSpoolProc);
- block.reference = refNum;
- block.spool.buffer = nil;
- block.spool.bufferSize = 0;
- dest = GXUnflattenShape(&block.spool, count, portList);
- DisposeRoutineDescriptor(block.spool.spoolProcedure);
- return dest;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- void GXStShapeToFile(gxShape source, Str255 fileName, short vRefNum, OSType creator, OSType fileType)
- {
- FSSpec spec;
- short err;
-
- err = FSMakeFSSpec(vRefNum,0,fileName,&spec);
- // IfDebug(err, "\pFSMakeFSSpec failed");
- GXStShapeToFSSpec(source, &spec, creator, fileType, 0);
- return;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- gxShape GXStFileToShape(Str255 fileName, short vRefNum, long count, const gxViewPort portList[])
- {
- FSSpec spec;
- short err;
-
- err = FSMakeFSSpec(vRefNum,0,fileName,&spec);
- // IfDebug(err, "\pFSMakeFSSpec failed");
- return GXStFSSpecToShape(&spec, count, portList);
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- void GXStShapeToFSSpec(gxShape source, FSSpec* spec, OSType creator, OSType fileType, ScriptCode script)
- {
- short refNum;
- short err;
-
- if (source == nil)
- {
- GXPostGraphicsError(shape_is_nil);
- return;
- }
-
- if (creator == 0) creator = 'sLib';
- if (fileType == 0) fileType = 'flat';
- err = FSpCreate(spec,creator,fileType,script);
- if (err == dupFNErr) {
- err = FSpDelete(spec);
- // IfDebug(err, "\pFSpDelete failed");
- err = FSpCreate(spec,creator,fileType,script);
- }
- // IfDebug(err, "\pFSpCreate failed");
- err = FSpOpenDF(spec,fsWrPerm,&refNum);
- // IfDebug(err, "\pFSpOpenDF failed");
- GXStShapeToFRef(source, refNum);
- err = FSClose(refNum);
- // IfDebug(err, "\pFSClose failed");
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- gxShape GXStFSSpecToShape(FSSpec* spec, long count, const gxViewPort portList[])
- {
- short refNum;
- short err;
- gxShape dest;
-
- if (spec == nil)
- {
- GXPostGraphicsError(parameter_is_nil);
- return nil;
- }
- err = FSpOpenDF(spec, fsCurPerm, &refNum);
- // IfDebug(err, "\pFSpOpenDF failed");
- dest = GXStFRefToShape(refNum, count, portList);
- err = FSClose(refNum);
- // IfDebug(err, "\pFSClose failed");
- return dest;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- Handle GXStFontToHandle(gxFont fontID, long glyphBits[])
- {
- userSpool block;
- scalerStream stream;
-
- block.spool.spoolProcedure = NewgxSpoolProc(HandleSpoolProc);
- block.spool.buffer = nil;
- block.spool.bufferSize = 0;
-
- stream.streamRefCon = fontID;
- stream.types = type1StreamType;
- stream.action = downloadStreamAction;
- stream.memorySize = 0;
- stream.variationCount = selectAllVariations;
- stream.variations = nil;
- stream.info.font.encoding = 0;
- stream.info.font.glyphBits = glyphBits;
- stream.info.font.name = (char*)"\pSkia-ps";
-
- GXFlattenFont(fontID, &stream, &block.spool);
- DisposeRoutineDescriptor(block.spool.spoolProcedure);
- return (Handle)block.data;
- }
-
- /* ---------------------------------------------------------------------------
-
- --------------------------------------------------------------------------- */
-
- gxFont GXStHandleToFont(Handle source)
- {
- /* GXNewFont does not copy the data in source, so you can't dispose of
- it until you have called GXDisposeFont().
- */
- return GXNewFont(gxHandleFontStorage, source, 0);
- }
-